package com.demo.controller;

import com.demo.entity.User;
import com.demo.response.Result;
import com.demo.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.security.NoSuchAlgorithmException;

/**
 * 用户信息表 前端控制器
 * @author ZM
 * @since 2020-11-23
 */
@Controller
@RequestMapping("/user")
@Api(tags = "用户类")
public class UserController {
    @Autowired
    IUserService userService;

    @ApiOperation("新增用户")
    @PostMapping("/addUser")
    @ResponseBody
    public Result addUser(@Validated @RequestBody User user, BindingResult result) throws Exception {
        if (result.hasErrors()){
            return Result.error(result.getFieldError().getDefaultMessage());
        }
        return userService.addUser(user);
    }

    @ApiOperation("编辑用户")
    @PostMapping("/updateUser")
    @ResponseBody
    public Result updateUser(@RequestBody User user, BindingResult result) throws Exception {
        if (result.hasErrors()){
            return Result.error(result.getFieldError().getDefaultMessage());
        }
        return userService.updateUser(user);
    }

    @ApiOperation("删除用户")
    @GetMapping("/deleteUser")
    @ResponseBody
    public Result deleteRole(@RequestParam(name = "userId")Integer userId,
                             @RequestParam(name = "deleteUserId")Integer deleteUserId){
        return userService.deleteUser(userId, deleteUserId);
    }

    @ApiOperation(value ="用户列表",tags={"用户类","v20210420更新"})
    @GetMapping("/userList")
    @ResponseBody
    public Result roleList(@RequestParam(name = "pageNumber",required = false,defaultValue = "1")Integer pageNumber,
                           @RequestParam(name = "pageSize",required = false,defaultValue = "10")Integer pageSize,
                           @RequestParam(name = "userId",required = false)Integer userId,
                           @RequestParam(name = "userLoginName",required = false)String userLoginName,
                           @RequestParam(name = "userTrueName",required = false)String userTrueName,
                           @RequestParam(name = "roleId",required = false)Integer roleId,
                           @RequestParam(name = "userType",required = false)Integer userType,
                           @RequestParam(name = "userMobile",required = false)String userMobile,
                           @RequestParam(name = "workSn",required = false)String workSn,
                           @RequestParam(name = "workStatus",required = false)Integer workStatus){
        return userService.userList(pageNumber, pageSize, userId, userLoginName, userTrueName, roleId, userType, userMobile, workSn, workStatus);
    }


}
